<template>
	
	<form @submit="formSubmit" report-submit='true'>
	<view class='addAddress'>
		<view class='list'>
			<view class='item'>
				<view class='name'>姓名</view>
				<input type='text' placeholder='请输入姓名' name='name' :value="name" placeholder-class='placeholder'></input>
			</view>
			<view class='item'>
				<view class='name'>联系电话</view>
				<input type='text' placeholder='请输入联系电话' name="mobile" :value='mobile' placeholder-class='placeholder'></input>
			</view>
			<view class='item'>
				<view class='name'>所在地区</view>
				<picker mode="region" @change="bindRegionChange" :value="region" :custom-item="customItem">
					<view class="right">
						<view class="picker">{{regions[0]}}，{{regions[1]}}，{{regions[2]}}</view>
						<view class='iconfont icon-dizhi font-color'></view>
					</view>
				</picker>
			</view>
			<view class='item'>
				<view class='name'>详细地址</view>
				<input type='text' placeholder='请填写具体地址' name='detail' placeholder-class='placeholder' :value='detail'></input>
			</view>
		</view>
		<view class='default'>
			<checkbox-group @change='changeIsDefault'><checkbox :checked="is_default ? true : false"/>设置为默认地址</checkbox-group>
		</view>
		<button class='keepBnt' formType="submit">立即保存</button>
	</view>
	</form>
</template>

<script>
	
	import { Address } from '@/utils/model/address-model.js';
	var address = new Address();
	
	export default {
		data() {
			return {
				len: 0,
				regions: ['省', '市', '区'],
				is_default: false,//地址详情  1默认 0非
				name:'',
				mobile:'',
				region:'',
				detail:''
			};
		},
		onLoad(options) {
		},
		methods: {
			bindRegionChange (e) {
				this.regions = e.detail.value
			},
			changeIsDefault(e){
				this.is_default = !this.is_default
			},
			formSubmit(e){
			  var that = this;
			  var value = e.detail.value;
			  console.log(e)
			  if (!value.name){
				uni.showToast({
				  title: '请填写收货人姓名',
				  icon: 'none'
				})
				return
			  }
			  if (!value.mobile){
				uni.showToast({
				  title: '请填写联系电话',
				  icon: 'none'
				})
				return
			  }
			  if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.mobile)){
				uni.showToast({
				  title: '请输入正确的手机号码',
				  icon: 'none'
				})
				return
			  }
			  if (that.regions[0] =='省'){
				uni.showToast({
				  title: '请选择所在地区',
				  icon: 'none'
				})
				return
			  }
			  if (!value.detail){
				uni.showToast({
				  title: '请填写详细地址',
				  icon: 'none'
				})
				return
			  }
			  value.province = that.regions[0];
			  value.city = that.regions[1];
			  value.region = that.regions[2];//地区
			  value.country = '中国';//国家
			  value.is_default = that.is_default ? 1 : 0;
			  //新增地址
			  address.add(value).then(res => {
				  console.log(res)
				  console.log('cccccccccccccccccccccccccccccccccccccccccccc')
				  if(res.code == 1){
					  uni.showToast({
						title: '添加成功',
						icon: 'success'
					  })
					  setTimeout(function () {
						uni.navigateBack({ delta: 1 });
					  }, 1000);
				  }
			  });

			}
		}
	}
</script>
 
<style>
	page{
		background-color: #F5F5F5;
	}
</style>
 
<style scoped lang="scss">
	@import '~@/pages/my/address/add.scss';
</style>

